package sls;

import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author huangya
 * @date 2023/4/20
 */
public class SlsLogExtract {

    public static void main(String[] args) {
        String url = "04-20 16:26:07\n" +
                "33.50.157.13\n" +
                "rdam033050157013.center.na610\n" +
                "/home/admin/logs/bi/bi_rdam_api.log\n" +
                "rdamhost\n" +
                "__tag__:__user_defined_id__:rdamhost\n" +
                "content:2023-04-20 16:26:07,077 -- {\"user_key\":\"23437264\",\"inter_name\":\"com.yunos.rdam.biz.service.openapi.impl.OpenGenericServiceImpl\",\"method\":\"invoke_b227\",\"cost\":\"690\",\"code\":\"200\",\"msg\":\"success\",\"ext_user_key\":\"zebra_gateway\",\"system\":\"rdam\",\"ip\":\"33.50.157.13\",\"log_type\":\"INTERFACE\",\"trace_id\":\"1879dc54e9d297e9eb9\",\"time\":\"1681979167077\",\"meruRouteId\":null,\"meruBucket\":null,\"params\":{\"data_size\":13,\"api_id\":\"private.cosmo.map.around.search\",\"request\":{\"conMap\":{\"top_appkey\":\"23437264\",\"keywords\":\"充电桩\",\"lng\":\"120.463945\",\"__charge_project__\":\"MF\",\"witFlag\":0,\"apiId\":\"private.cosmo.map.around.search\",\"lat\":\"31.2302791\",\"userLoc\":\"120.44431027777777,31.32284888888889\"},\"traceId\":\"1879dc54e9d297e9eb9\"},\"e_trace\":\"21057ba616819791663854142e9330\",\"route_id\":\"private.cosmo.map.around.search#EMLCA8W0WECNYCKQP2IF\",\"user_key\":\"23437264\",\"source_ip\":\"33.79.139.40\",\"ext_user_key\":\"zebra_gateway\",\"rpc_id\":\"4608\",\"parent_id\":\"\",\"source_app\":\"top\",\"root_id\":\"private.cosmo.map.around.search\",\"c_device_id\":\"\",\"app_id\":\"meta\"}}";
        // 编写正则表达式
        String regFileName = "\"request\":\\{\"conMap\":.*,\"e_trace\":\"";
        // 匹配当前正则表达式
        Matcher matcher = Pattern.compile(regFileName).matcher(url);
        // 定义当前文件的文件名称
        String fileName = "";
        // 判断是否可以找到匹配正则表达式的字符
        while (matcher.find()) {
            // 将匹配当前正则表达式的字符串即文件名称进行赋值
            fileName = matcher.group();
            if(StringUtils.isNotEmpty(fileName) && fileName.length()>12){
                fileName = fileName.substring(10, fileName.length()-12);
            }
        }
        System.out.println(fileName);
    }
}
